package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.BrandService;
import com.spzx.product.service.CategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author Haoran Sun
 * @since 2024-07-15
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {

    @Resource
    private CategoryBrandMapper categoryBrandMapper;
    @Resource
    private CategoryService categoryService;
    @Resource
    private BrandService brandService;

    @Override
    public List<CategoryBrand> listCategoryBrand(CategoryBrand categoryBrand) {
        return categoryBrandMapper.selectListCategoryBrand(categoryBrand);
    }

    @Override
    public CategoryBrand getCateGoryBrandById(Long id) {
        CategoryBrand categoryBrand = categoryBrandMapper.selectById(id);
        LinkedList<Long> categoryIdList = new LinkedList<>();
        categoryService.getParentCategory(categoryBrand.getCategoryId(), categoryIdList);
        categoryBrand.setCategoryIdList(categoryIdList);
        return categoryBrand;
    }
    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        //判断categoryBrand是否已经存在
        if(categoryBrandMapper.selectCount(
                new LambdaQueryWrapper<CategoryBrand>()
                        .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                        .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
        ) > 0) throw new RuntimeException("该分类下此品牌已经存在");
        return categoryBrandMapper.insert(categoryBrand);
    }

    @Override
    public int updateCategoryBrandById(CategoryBrand categoryBrand) {
        //判断categoryBrand是否没变
        CategoryBrand cbData = categoryBrandMapper.selectById(categoryBrand.getId());
        if (cbData.getBrandId().equals(categoryBrand.getBrandId())
                && cbData.getCategoryId().equals(categoryBrand.getCategoryId())
        ){return 1;}
        //判断categoryBrand是否已经存在
        if(categoryBrandMapper.selectCount(
                new LambdaQueryWrapper<CategoryBrand>()
                        .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                        .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
        ) > 0) throw new RuntimeException("该分类下此品牌已经存在");

        return categoryBrandMapper.updateById(categoryBrand);
    }

    @Override
    public List<Brand> getBrandListByCategoryId(Long categoryId) {
        List<CategoryBrand> categoryBrands = categoryBrandMapper.selectList(new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getCategoryId, categoryId)
                .select(CategoryBrand::getBrandId)
        );
        if (!categoryBrands.isEmpty()) {
            List<Long> brandIdList = categoryBrands.stream().map(CategoryBrand::getBrandId).toList();
            return brandService.listByIds(brandIdList);
        }
        throw new ServiceException("该分类下没有品牌");
    }
}
